//Design By Do Trung --- Email : trungqn19852003@yahoo.com
//Ngày Design :9/7/2011 10:04:46 AM
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Linq;
using System.Collections.Generic;

using VietStar.Library.Commom.SqlServer;
using Entity = VietStar.Library.Entity;

namespace VietStar.Library.Data
{
	/// <summary>
	/// </summary>
	public class QuyenTruyCap
    {
		public QuyenTruyCap() {}
		Connection db = new Connection();
		Entity.QuyenTruyCap quyentruycap = new Entity.QuyenTruyCap();

        /// <summary>
        /// Inserts a record into the QuyenTruyCap table.
        /// <summary>
        /// <param name="Id"></param>
        /// <param name="TenQuyen"></param>
        /// <returns></returns>
	    public bool Insert(Entity.QuyenTruyCap entity)
	    {
		    bool boolInsert = false;
		    SqlConnection SqlCnn = db.ReadFromWebConfig();
		    SqlDataAdapter SqlAdp = new SqlDataAdapter();
		    SqlCommand SqlCmd = new SqlCommand();
		    SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		    try
		    {
			    SqlCmd.Transaction = SqlTrans;
			    SqlCmd.Connection = SqlCnn;
			    SqlCmd.CommandText="Proc_QuyenTruyCap_Insert";
			    SqlCmd.CommandType=CommandType.StoredProcedure;
			    SqlCmd.Parameters.Add("@Id", SqlDbType.Int).Value=entity.Id;
			    SqlCmd.Parameters.Add("@TenQuyen", SqlDbType.NVarChar,50).Value=entity.TenQuyen;

			    SqlAdp.InsertCommand = SqlCmd;
			    SqlAdp.InsertCommand.ExecuteNonQuery();
			    SqlCmd.Transaction.Commit();
			    boolInsert = true;
			    SqlCnn.Close();
		    }
		    catch
		    {
			    boolInsert = false;
			    SqlCmd.Transaction.Rollback();
		    }
		    finally
		    {
			    SqlCmd.Parameters.Clear();
			    SqlTrans = null;
		    }
		    if (SqlCmd != null)
			    SqlCmd.Dispose();
		    if (SqlAdp != null)
			    SqlAdp.Dispose();
		    return boolInsert;
	    }

        /// <summary>
        /// Selects a single record from the QuyenTruyCap table.
        /// <summary>
        /// <returns>DataTable</returns>
	    public DataTable Select()
		{
		    DataTable dt = new DataTable();
		    SqlConnection SqlCnn = db.ReadFromWebConfig();
		    SqlDataAdapter SqlAdp = new SqlDataAdapter();
		    SqlCommand SqlCmd = new SqlCommand();
		    SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		    try
		    {
			    SqlCmd.Transaction = SqlTrans;
			    SqlCmd.Connection = SqlCnn;
			    SqlCmd.CommandText="Proc_QuyenTruyCap_Select";
			    SqlCmd.CommandType=CommandType.StoredProcedure;
			    SqlAdp.SelectCommand = SqlCmd;
			    SqlAdp.SelectCommand.ExecuteNonQuery();
			    SqlAdp.Fill(dt);
			    SqlCmd.Transaction.Commit();
			    SqlCnn.Close();
		    }
		    catch
		    {
			    SqlCmd.Transaction.Rollback();
		    }
		    finally
		    {
			    SqlCmd.Parameters.Clear();
			    SqlTrans = null;
		    }
		    if (SqlCmd != null)
			    SqlCmd.Dispose();
		    if (SqlAdp != null)
			    SqlAdp.Dispose();
		    return dt;
	    }

        /// <summary>
        /// Selects a single record from the QuyenTruyCap table.
        /// <summary>
        /// <returns>DataTable</returns>
	    public DataTable SelectPrimaryKey()
	    {
		    DataTable dt = new DataTable();
			SqlConnection SqlCnn = db.ReadFromWebConfig();
			SqlDataAdapter SqlAdp = new SqlDataAdapter();
			SqlCommand SqlCmd = new SqlCommand();
			SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		    try
		    {
			    SqlCmd.Transaction = SqlTrans;
			    SqlCmd.Connection = SqlCnn;
			    SqlCmd.CommandText="Proc_QuyenTruyCap_SelectPrimaryKey";
			    SqlCmd.CommandType=CommandType.StoredProcedure;
			    SqlCmd.Parameters.Add("@Id", SqlDbType.Int).Value=quyentruycap.Id;

			    SqlAdp.SelectCommand = SqlCmd;
			    SqlAdp.SelectCommand.ExecuteNonQuery();
			    SqlAdp.Fill(dt);
			    SqlCmd.Transaction.Commit();
			    SqlCnn.Close();
		    }
		    catch
		    {
			    SqlCmd.Transaction.Rollback();
		    }
		    finally
		    {
			    SqlCmd.Parameters.Clear();
			    SqlTrans = null;
		    }
		    if (SqlCmd != null)
			    SqlCmd.Dispose();
		    if (SqlAdp != null)
			    SqlAdp.Dispose();
		    return dt;
	    }

        /// <summary>
        /// Updates a record in the QuyenTruyCap table.
        /// <summary>
        /// <param name="Id"></param>
        /// <param name="TenQuyen"></param>
	    public bool Update(Entity.QuyenTruyCap entity)
	    {
		    bool boolUpdate = false ;
		    SqlConnection SqlCnn = db.ReadFromWebConfig();
		    SqlDataAdapter SqlAdp = new SqlDataAdapter();
		    SqlCommand SqlCmd = new SqlCommand();
		    SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		    try
		    {
			    SqlCmd.Transaction = SqlTrans;
			    SqlCmd.Connection = SqlCnn;
			    SqlCmd.CommandText="Proc_QuyenTruyCap_Update";
			    SqlCmd.CommandType=CommandType.StoredProcedure;
			    SqlCmd.Parameters.Add("@Id", SqlDbType.Int).Value=entity.Id;
			    SqlCmd.Parameters.Add("@TenQuyen", SqlDbType.NVarChar,50).Value=entity.TenQuyen;

			    SqlAdp.UpdateCommand = SqlCmd;
			    SqlAdp.UpdateCommand.ExecuteNonQuery();
			    SqlCmd.Transaction.Commit();
			    boolUpdate = true ;
			    SqlCnn.Close();
		    }
		    catch
		    {
			    boolUpdate = false ;
			    SqlCmd.Transaction.Rollback();
		    }
		    finally
		    {
			    SqlCmd.Parameters.Clear();
			    SqlTrans = null;
		    }
		    if (SqlCmd != null)
			    SqlCmd.Dispose();
		    if (SqlAdp != null)
			    SqlAdp.Dispose();
		    return boolUpdate;
	    }

        /// <summary>
		/// Deletes a record from the QuyenTruyCap table by a composite primary key.
        /// <summary>
	    public bool Delete()
	    {
		    bool boolDelete = false;
		    SqlConnection SqlCnn = db.ReadFromWebConfig();
		    SqlDataAdapter SqlAdp = new SqlDataAdapter();
		    SqlCommand SqlCmd = new SqlCommand();
		    SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		    try
		    {
			    SqlCmd.Transaction = SqlTrans;
			    SqlCmd.Connection = SqlCnn;
			    SqlCmd.CommandText="Proc_QuyenTruyCap_Delete";
			    SqlCmd.CommandType=CommandType.StoredProcedure;
			    SqlCmd.Parameters.Add("@Id", SqlDbType.Int).Value=quyentruycap.Id;

			    SqlAdp.DeleteCommand = SqlCmd;
			    SqlAdp.DeleteCommand.ExecuteNonQuery();
			    SqlCmd.Transaction.Commit();
			    boolDelete = true ;
			    SqlCnn.Close();
		    }
		    catch
		    {
			    boolDelete = false ;
			    SqlCmd.Transaction.Rollback();
		    }
		    finally
		    {
			    SqlCmd.Parameters.Clear();
			    SqlTrans = null;
		    }
		    if (SqlCmd != null)
			    SqlCmd.Dispose();
		    if (SqlAdp != null)
			    SqlAdp.Dispose();
		    return boolDelete;
	    }
    }
}
